public class FarmProductsView {
    private FarmProductsService farmProductsService;



    public FarmProductsView() {
        farmProductsService = new FarmProductsService();
        farmProductsService.init(10);
    }

    public void enterMainMenu() {
        boolean loop = true;
        System.out.println("===============农产品信息管理软件===============");
        while (loop) {
            printMainMenu();
            char key = CMUtility.readMenuSelection();
            switch (key) {
                case '1' -> addNewFarmProducts();
                case '2' -> modifyFarmProducts();
                case '3' -> deleteFarmProducts();
                case '4' -> listAllFarmProducts();
                case '5' -> {
                    System.out.println("欢迎本次使用，再见！");
                    loop = false;
                }
                default -> System.out.print("输入错误！请重新选择：");
            }
        }
    }

    private void printMainMenu() {
        System.out.print("""
                    
                ---------------------菜单---------------------
                1. 添加农产品
                2. 修改农产品
                3. 删除农产品
                4. 农产品列表
                5. 退出
                
                请选择(1-5)：""");
    }

    private void addNewFarmProducts() {
        System.out.println("---------------------添加农产品---------------------");
        FarmProducts farmProduct = readFarmProductDetails();
        farmProductsService.addFarmProducts(farmProduct);
        System.out.println("---------------------添加完成---------------------\n");
    }

    private FarmProducts readFarmProductDetails() {
        FarmProducts farmProduct = new FarmProducts();
        System.out.print("品种：");
        farmProduct.setType(CMUtility.readString(5));
        System.out.print("数量：");
        farmProduct.setCount(CMUtility.readInt());
        System.out.print("上市时间：");
        farmProduct.setDate(CMUtility.readString(11));
        System.out.print("报价：");
        farmProduct.setPrice(CMUtility.readDouble());
        return farmProduct;
    }

    private void modifyFarmProducts() {
        if (farmProductsService.isEmpty()) {
            System.out.println("当前农产品列表为空, 已退出修改!");
            return;
        }
        System.out.println("---------------------修改农产品---------------------");
        System.out.print("请选择待修改农产品编号(-1退出)：");
        int index = readFarmProductIndex();
        if (index == -1) {
            System.out.println("已取消修改。");
            return;
        }
        FarmProducts farmProduct = farmProductsService.getFarmProducts(index);
        System.out.printf("查询到了%s产品\n", farmProduct.getType());
        updateFarmProductDetails(farmProduct);
        System.out.println("---------------------修改完成---------------------\n");
    }

    private int readFarmProductIndex() {
        while (true) {
            int index = CMUtility.readInt();
            if (index == -1 || (index >= 1 && index <= farmProductsService.size())) {
                return index - 1;
            } else {
                System.out.println("输入编号有误, 未查询到该农产品, 请重新输入");
            }
        }
    }

    private void updateFarmProductDetails(FarmProducts farmProduct) {
        System.out.printf("品种(%s)：<直接回车表示不修改>", farmProduct.getType());
        String oldName = farmProduct.getType();
        farmProduct.setType(CMUtility.readString(5, oldName));
        System.out.printf("数量(%d)：", farmProduct.getCount());
        int oldCount = farmProduct.getCount();
        farmProduct.setCount(CMUtility.readInt(oldCount));
        System.out.printf("上市时间(%s)：", farmProduct.getDate());
        String oldDate = farmProduct.getDate();
        farmProduct.setDate(CMUtility.readString(11, oldDate));
        System.out.printf("报价(%.3f元/斤)：", farmProduct.getPrice());
        double oldPrice = farmProduct.getPrice();
        farmProduct.setPrice(CMUtility.readDouble(oldPrice));
    }

    private void deleteFarmProducts() {
        if (farmProductsService.isEmpty()) {
            System.out.println("当前农产品列表为空, 已退出删除!");
            return;
        }
        System.out.println("---------------------删除农产品---------------------");
        System.out.print("请选择待删除农产品编号(-1退出)：");
        int index = readFarmProductIndex();
        if (index == -1) {
            System.out.println("已取消删除。");
            return;
        }
        FarmProducts farmProduct = farmProductsService.getFarmProducts(index);
        System.out.printf("查询到了%s产品\n", farmProduct.getType());
        confirmFarmProductDeletion(index);
    }

    private void confirmFarmProductDeletion(int index) {
        System.out.print("确认是否删除(Y/N)：");
        char confirmation = CMUtility.readConfirmSelection();
        if (confirmation == 'Y') {
            farmProductsService.deleteFarmProducts(index);
            System.out.println("---------------------删除完成---------------------\n");
        } else {
            System.out.println("已取消删除");
        }
    }

    private void listAllFarmProducts() {
        if (farmProductsService.isEmpty()) {
            System.out.println("当前农产品列表为空, 已退出查询!");
            return;
        }
        FarmProducts[] farmProducts = farmProductsService.getAllFarmProducts();
        if (farmProducts == null) {
            System.out.println("当前农产品列表为空, 已退出查询!");
            return;
        }
        System.out.println("---------------------------农产品列表---------------------------");
        System.out.print("编号\t品种\t数量\t上市时间\t\t\t报价\n");
        for (int i = 0; i < farmProducts.length; i++) {
            System.out.println(String.format("%-8s", i + 1) + farmProducts[i]);
        }
        System.out.println("-------------------------农产品列表完成-------------------------\n");
    }
}
